<template>
	<div style="padding-bottom: 20px;">
		<el-tree
		  :data="authData"
		  show-checkbox
		  node-key="id"
		  ref="tree"
		  highlight-current>
		</el-tree>
		<br>
		<span style="float: right;" class="dialog-footer">
		        <el-button  type="primary" size="mini" @click="confirm">
					确 定
				</el-button>
		    </span>
	</div>
</template>

<script>
	export default {
		data(){
			return {
				authData: [],
			}
		},
		props:{
			id:{
				type:Number,
				default:0
			}
		},
		mounted() {
			this.lodeMenuList()
		},
		watch:{
			
		},
		methods:{
			confirm(){
				let checkNodeArr = this.$refs.tree.getCheckedNodes()
				if(checkNodeArr.length==0) return this.$message.error('请选择权限')
				let checkNode = checkNodeArr.map(item=>{
					return item.id
				})
				checkNodeArr = checkNodeArr.filter((item)=>{
					return item.id>8
				})
				
				for(let item of checkNodeArr){
					console.log(item)
					let index = checkNode.findIndex(el=>{
						return el==item.parent_id
					})
					if(index==-1){
						checkNode.push(item.parent_id)
					}
				}
				
				checkNode = checkNode.join(',')
				
				let params = {
					id:this.id,
					checkNode:checkNode
				}
				this.$post('/system/updateAuth',params,res=>{
					this.$emit('close')
				})
				
				
			},
			lodeMenuList(){
				this.$post('/system/getMenuList',{id:this.id},res=>{
					let result = res.data.data
					for(let item of result.result){
						item.label = item.title
						for(let ele of item.children){
							ele.label = ele.title
						}
					}
					this.authData = result.result
					if(result.checkNode){
						let arr = result.checkNode.split(',')
						arr = arr.filter((el)=>{
							return el>8
						})
						this.$refs.tree.setCheckedKeys(arr);
					}
					 
				})
			},
		}
	}
</script>

<style>
</style>